class Solution(object):
    def maxSumDivThree(self, nums):
        nums.sort()
        t = sum(nums)
        if t % 3 == 0:
            return t
        if t % 3 == 1:
            min_1 = float('inf')
            c_2 = 0
            min_2 = 0
            for num in nums:
                if num % 3 == 1:
                    min_1 = min(min_1, num)
                if num % 3 == 2:
                    if c_2 < 2:
                        c_2 += 1
                        min_2 += num
            if c_2 == 2:
                return t - min(min_1, min_2)
            else:
                return t - min_1
        if t % 3 == 2:
            min_1 = 0
            c_1 = 0
            min_2 = float('inf')
            for num in nums:
                if num % 3 == 1:
                    if c_1 < 2:
                        c_1 += 1
                        min_1 += num
                if num % 3 == 2:
                    min_2 = min(min_2, num)
            if c_1 == 2:
                return t - min(min_1, min_2)
            else:
                return t - min_2

